﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Scientific.Communication;
using Walson.Scientific;
//using Walson.Net.UdpClientCompact;
using Walson.Net.Sockets;
using Walson.IO.Adapter;

namespace Test
{
    public partial class Form1 : Form
    {
        //UdpSerialBridge _bridge;
        UdpClientCompact _udp;

        UdpSerialServer _server;

       public Form1()
       {
           CheckForIllegalCrossThreadCalls = false;
           InitializeComponent();

           _udp = new UdpClientCompact(10000);
           _udp.DataReceived += new UdpClientCompact.DataReceivedHandler(_udp_DataReceived);
           _udp.EnableAutoRecieve = true;
           //_udp.StartAutoReceive();

//            _bridge = new UdpSerialBridge(9090, "COM8", 9600);
//            _bridge.Start();

           _server = new UdpSerialServer();
           _server.Start();
       }

       void _udp_DataReceived(object sender, EventArgs e)
       {
           textBox1.AppendText(Encoding.Default.GetString(_udp.ReadMessage().MessageBody));
       }



        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //_udp.SendTo("192.168.1.100", 9090, "Hello!\r\n");
            //timer1.Enabled = !timer1.Enabled;
            _udp.SendTo("192.168.1.100", 9090, "GET COMLIST");
            //_udp.SendTo("192.168.1.100", 9090, "help");
            _udp.SendTo("192.168.1.100", 9090, "set com5 4800 30000 192.168.1.100 10000 100 10");
            _udp.SendTo("192.168.1.100", 9090, "set com8 115200 30001 192.168.1.100 10000 100 10");

            _udp.SendTo("192.168.1.100", 9090, "GET COMLIST");
        }

        int i = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            _udp.SendTo("192.168.1.100", 9090, "GET COMLIST");
            i++;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //_udp.SendTo("192.168.1.100", 10000, "GET COMLIST");
//             if (_bridge.IsRunning)
//                 _bridge.Stop();
//             else
//                 _bridge.Start();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            _udp.SendTo("192.168.1.100", 20000, "ffff");
            _udp.SendTo("192.168.1.100", 20001, "ffff");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            _udp.SendTo("192.168.1.100", 30000, "ffff");
            _udp.SendTo("192.168.1.100", 30001, "ffff");
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            MessageBox.Show("FFF");
        }

        
    }
}
